<template>
  <div class="app-container">
    <div class="btn-add">
      <el-button type="primary" size="mini" @click="addMessage">新增</el-button>
    </div>
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item prop="creatorRealName">
        <el-input v-model="queryParams.creatorRealName" placeholder="发起人" clearable size="small" />
      </el-form-item>
      <el-form-item>
        <el-input v-model="queryParams.replierRealName" clearable placeholder="回复人" />
      </el-form-item>
      <el-form-item prop="diseasesHistory">
        <div class="project-select" @click="selectProject">
          <span>
            <span style="color: #606266;" v-if="projectName">{{projectName}}</span>
            <span style="color: #C0C4CC;" v-else>项目名称</span>
          </span>
          <span v-if="projectName" class="icon" style="color: #C0C4CC;" @click.stop="clearProject"><i class="el-icon-circle-close"></i></span>
        </div>
      </el-form-item>
      <el-form-item prop="isReply">
        <el-select v-model="queryParams.isReply" placeholder="状态" clearable size="small">
          <el-option v-for="dict in statusList" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="tableData">
      <el-table-column label="留言内容" width="200" align="center" show-overflow-tooltip prop="content" />
      <el-table-column label="关联项目" align="center" prop="projectName" width="200" />
      <el-table-column label="发起人" align="center" prop="creatorRealName" />
      <el-table-column label="回复人" align="center" prop="replierRealNameList">
        <template v-slot="scope">
          <div v-if="scope.row.replierRealNameList">{{scope.row.replierRealNameList[0]}}</div>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="isReply" width="200">
        <template v-slot="scope">
          <span>{{ scope.row.isReply === 1 ? '已回复' : '未回复'}}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column label="更新时间" align="center" prop="updateTime" />
      <el-table-column label="操作" align="center" width="100">
        <template v-slot="scope">
          <el-button size="mini" @click="detailClick(scope.row)">详情</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="pagination.pageNum"
      :limit.sync="pagination.pageSize"
      @pagination="pageHandler"
    />
    <add-side ref="addSideRef" @update="handleQuery" />
    <ProjectConfigModal ref="projectConfigRef" @update="changeProject" />
  </div>
</template>
<script>
import AddSide from './addSide.vue'
import { getProjectMessage } from '@/api/singleraView/message'
import ProjectConfigModal from '@/views/auditManagement/projectConfiguration/projectConfigModal.vue'
export default {
  name: "UserAdvice",
  components: {
    ProjectConfigModal,
    AddSide
  },
  data () {
    return {
      // 遮罩层
      loading: false,
      // 总条数
      total: 0,
      // 参数表格数据
      tableData: [],
      pagination: {
        pageNum: 1,
        pageSize: 10,
      },
      projectName: '',
      statusList: [
        {label: '全部', value: ''},
        {label: '已回复', value: 1},
        {label: '未回复', value: 0}
      ],
      // 查询参数
      queryParams: {
        isReply: '',
        replierRealName: '',
        creatorRealName: '',
        projectId: '',
      },
    };
  },
  activated() {
    this.handleQuery()
  },
  created() {
    let token = this.$route.query
    if(token) {
      if(token.icsAuthorization) {
        localStorage.setItem('icsAuthorization', decodeURIComponent(token.icsAuthorization))
      }
      if(token.authorization) {
        localStorage.setItem('authorization', decodeURIComponent(token.authorization))
      }
    }
    this.handleQuery()
  },
  methods: {
    getList () {
      let params = {
        ...this.queryParams,
        ...this.pagination
      }
      this.loading = true
      getProjectMessage(params).then(res => {
        this.loading = false
        this.tableData = res.rows
        this.total = res.total
      })
    },
    addMessage() {
      this.$refs.addSideRef.init()
    },
    selectProject(){
      this.$refs.projectConfigRef.getInit()
    },
    clearProject(){
      this.projectName = ''
      this.queryParams.projectId = ''
    },
    changeProject(item){
      this.queryParams.projectId = item.value
      this.projectName = item.name
    },
    handleQuery () {
      this.pagination.pageNum = 1
      this.pagination.pageSize = 10
      this.getList()
    },
    pageHandler(item) {
      this.pagination.pageNum = item.page
      this.pagination.pageSize = item.limit
      this.getList()
    },
    resetQuery () {
      this.queryParams = {
        replierRealName: '',
        isReply: '',
        creatorRealName: '',
        projectId: '',
      }
      this.pagination = {
        pageNum: 1,
        pageSize: 10,
      }
      this.projectName = ''
      this.handleQuery()
    },
    detailClick(row) {
      let params = {
        projectId: row.projectId,
        projectName: row.projectName,
        content: row.content,
        creatorRealName: row.creatorRealName,
        createTime: row.createTime,
      }
      this.$refs.addSideRef.init(row.projectMessageId, params)
    }
  }
}
</script>
<style lang="scss" scoped>
.app-container{
  padding-top: 35px;
  position: relative;
  .btn-add{
    position: absolute;
    right: 10px;
    top: 10px;
  }
  .el-form-item{
    margin-bottom: 10px;
  }
}
.project-select{
  width: 200px;
  height: 34px;
  background: #FFFFFF;
  border-radius: 5px;
  color: #C0C4CC;
  padding: 0 20px;
  cursor: pointer;
  border: 1px solid #DCDFE6;
  position: relative;
  overflow: hidden;
  .icon{
    position: absolute;
    right: 5px;
    top: 0;
  }
}
</style>
